package model.net.handlers
{
	import flash.utils.ByteArray;
	
	import global.GameState;
	
	import model.card.ICardData;
	import model.net.AbsDataHandler;
	import model.net.NetDataType;
	import model.play.RecordCardData;
	import model.result.ResultData;
	import model.result.RoundResultData;
	import model.role.IRoleData;
	import model.role.RoleData;
	import model.room.FriendData;
	import model.room.ItemData;
	import model.room.MailData;
	import model.room.RoomData;
	import model.sit.ISitState;
	import model.sit.SitInfoData;
	import model.sit.SitState;
	
	import utils.ai.CardTools;
	
	public class DataRoomHandler extends AbsDataHandler
	{
		public function DataRoomHandler()
		{
			register(NetDataType.SC_ROOM_INFO,handleRoomInfo);
			register(NetDataType.SC_MONEY_MSG,moneyMsg);
			register(NetDataType.SC_AUTO_MSG,autoMsg);
			register(NetDataType.SC_SIT_STATE,sitState);
			register(NetDataType.SC_START_ROUND,startRound);
			register(NetDataType.SC_SIT_INFO,sitInfo);
			register(NetDataType.SC_MULTI_LEFT_CARDS,multiLeftCards);
			register(NetDataType.SC_GIVE_PLAYER_CARDS,givePlayerCards);
			register(NetDataType.SC_SHOW_TABLE_CARDS,showTableCards);
			register(NetDataType.SC_CLEAR_ROUND_CARDS,clearRoundShowCards);
			register(NetDataType.SC_STAND_UP,standUp);
			register(NetDataType.SC_ROUND_RESULT,roundResult);
			register(NetDataType.SC_CHAT_FACE,chatFace);
			register(NetDataType.SC_TABLE_ANIMATION,tableAnimation);
			register(NetDataType.SC_FRIEND_APPLY,friendApply);
			register(NetDataType.SC_SEND_MAIL,sendMailAnimation);
			register(NetDataType.SC_FRIEND_LIST,friendList);
			register(NetDataType.SC_GO_HALL,gotoHall);
			register(NetDataType.SC_ITEM_LIST,itemOwnList);
			register(NetDataType.SC_REMBER_CARD,recordCard);
			register(NetDataType.SC_ITEM_ANIMATION,itemAnimate);
			register(NetDataType.SC_SHOP_ITEM_LIST,onShopItemList);
			register(NetDataType.SC_TASK_LIST,taskList);
			register(NetDataType.SC_SIT_ANIMATION,sitAnimation);
			register(NetDataType.SC_USE_ITEM_RESULT,useItemResult);
			register(NetDataType.SC_PLAYER_INFO,sitInfoDetail);
			register(NetDataType.SC_BROKE,broke);
			register(NetDataType.SC_MONEY_ANIMATION,sitMoneyAnimate);
			register(NetDataType.SC_HEART_BEAT,heartBeat);
		}
		
		//心跳
		private function heartBeat(byteArray:ByteArray):void
		{
			_model.netRemote.sendHeartBeat();
		}
		
		//座位加减钱动画
		private function sitMoneyAnimate(byteArray:ByteArray):void
		{
			var sitId:int = byteArray.readShort();
			var delta:int = byteArray.readInt();
			
			_model.sitModel.updateMoneyChangeAnimate(sitId,delta);
		}
		
		//破产
		private function broke(byteArray:ByteArray):void
		{
			_model.playerModel.broke();
		}
		
		//座位详细信息
		private function sitInfoDetail(byteArray:ByteArray):void
		{
			var data:SitInfoData = new SitInfoData();
			data.bigImage = readString(byteArray);
			data.exp = int(readString(byteArray));
			data.nextExp = int(readString(byteArray));
			data.level = byteArray.readShort();
			data.homeTown = readString(byteArray);
			data.snsName = readString(byteArray);
			data.company = readString(byteArray);
			data.playerId = byteArray.readInt();
			data.playerName = readString(byteArray);
			data.sex = byteArray.readShort();
			data.money = readString(byteArray);
			
			_model.sitModel.updateSitInfo(data);
		}
		
		//使用物品结果
		private function useItemResult(byteArray:ByteArray):void
		{
			var itemId:int = byteArray.readShort();
			var count:int = byteArray.readShort();
			_model.itemModel.recieveUseResult(itemId,count);
		}
		
		//座位动画
		private function sitAnimation(byteArray:ByteArray):void
		{
			var fromSitId:int = byteArray.readShort();
			var toSitId:int = byteArray.readShort();
			var animateId:int = byteArray.readShort();
			
			_model.sitModel.sitAnimation(fromSitId,toSitId,animateId);
		}
	
		
		//任务
		private function taskList(byteArray:ByteArray):void
		{
			
			var taskDailyXml:XML = new XML("<taskList></taskList>");
			var taskOnceXml:XML = new XML("<taskList></taskList>");
			var name:String;
			var progress:String;
			var bonuse:String;
			var desc:String;
			var dailyCount:int = byteArray.readShort();
			for (var i:int=0; i<dailyCount; i++)
			{
				
				name = readString(byteArray);
				progress = readString(byteArray);
				bonuse = readString(byteArray);
				desc = readString(byteArray);
				taskDailyXml.appendChild(new XML("<task name='" + name + "' bonuse='" + bonuse +"' progress='" + progress + "' desc='" + desc +"' />"));
			}
			
			var onceCount:int = byteArray.readShort();
			for (var n:int=0; n<onceCount; n++)
			{
				name = readString(byteArray);
				progress = readString(byteArray);
				bonuse = readString(byteArray);
				desc = readString(byteArray);
				taskOnceXml.appendChild(new XML("<task name='" + name + "' bonuse='" + bonuse +"' progress='" + progress + "' desc='" + desc +"' />"));
			}
			
			_model.taskModel.updateGameDailyXml(taskDailyXml);
			_model.taskModel.updateGameOnceXml(taskOnceXml);
		}
		
		//商店物品列表
		private function onShopItemList(byteArray:ByteArray):void
		{
			var count:int = byteArray.readShort();
			var itemData:ItemData;
			var items:Array = [];
			for (var i:int=0;i<count; i++)
			{
				itemData = new ItemData();
				itemData.id = byteArray.readShort();
				itemData.name = readString(byteArray);
				itemData.image = readString(byteArray);
				itemData.desc = readString(byteArray);
				itemData.price = readString(byteArray);
				items.push(itemData);
			}
			
			_model.playerModel.showShopItemList(items);
			
		}
		
		//使用物品动画
		private function itemAnimate(byteArray:ByteArray):void
		{
			var sitId:String = readString(byteArray);
			var animateId:String = readString(byteArray);
			
			_model.sitModel.updateSitItemAnimate(sitId,animateId);
			_model.traceManager.out("card record animateId:",animateId);
		}
		
		//记牌器
		private function recordCard(byteArray:ByteArray):void
		{
			var count:int = byteArray.readShort();
			var recordCardData:RecordCardData;
			var datas:Array = [];
			for (var i:int=0; i<count; i++)
			{
				recordCardData = new RecordCardData();
				recordCardData.value = byteArray.readShort();
				recordCardData.count = byteArray.readShort();
				_model.traceManager.out("cardR:",recordCardData.value,recordCardData.count);
				datas.push(recordCardData);
			}
			
			_model.playerModel.showRecordCards(datas);
			_model.traceManager.out("card record count:",count);
		}
		
		//自己物品列表
		private function itemOwnList(byteArray:ByteArray):void
		{
			var itemDatas:Array = [];
			
			var count:int = byteArray.readShort();
			var itemData:ItemData
			for (var i:int=0;i<count;i++)
			{
				itemData = new ItemData();
				itemData.id = byteArray.readShort();
				itemData.name = readString(byteArray);
				itemData.image = readString(byteArray);
				itemData.desc = readString(byteArray);
				itemData.canUse = byteArray.readShort();
				itemData.count = byteArray.readShort();
				itemData.index = byteArray.readShort();
				
				itemDatas.push(itemData);
			}
			
			_model.itemModel.recieveItem(itemDatas);
			
			_model.traceManager.out("itemList:",count);
		}
		
		//去大厅
		private function gotoHall(byteArray:ByteArray):void
		{
			_model.tableModel.updateGotoHall();
		}
		
		//好友列表
		private function friendList(byteArray:ByteArray):void
		{
			var datas:Array = [];
			var count:int = byteArray.readShort();
			var friendData:FriendData;
			for (var i:int=0; i<count; i++)
			{
				friendData = new FriendData();
				friendData.type =0;
				friendData.playerId = byteArray.readInt();
				friendData.name = readString(byteArray);
				friendData.room = readString(byteArray);
				friendData.image = readString(byteArray);
				datas.push(friendData);
			}
			
			count = byteArray.readShort();
			for (i=0; i<count; i++)
			{
				friendData = new FriendData();
				friendData.type = 1;
				friendData.playerId = byteArray.readInt();
				friendData.name = readString(byteArray);
				friendData.room = readString(byteArray);
				friendData.image = readString(byteArray);
				datas.push(friendData);
			}
			_model.friendModel.updateDatas(datas);
		}
		
		//信封动画
		private function sendMailAnimation(byteArray:ByteArray):void
		{
			var fromSitId:int = int(readString(byteArray));
			var toSitId:int = int(readString(byteArray));
			_model.mailModel.sendMailAnimation(fromSitId,toSitId);
		}
		
		//好友申请信息
		private function friendApply(byteArray:ByteArray):void
		{	
			var mailData:MailData = new MailData();
			mailData.inviteId = readString(byteArray);
			mailData.name = readString(byteArray);
			mailData.image = readString(byteArray);
			_model.mailModel.addMail(mailData);
		}
		
		//桌子中间动画
		private function tableAnimation(byteArray:ByteArray):void
		{
			var effectId:String = readString(byteArray);
			var sitId:int = int(readString(byteArray));
			var soundId:String = readString(byteArray);
			if (sitId == -1)
			{
				_model.tableModel.updateTableEffect(effectId,soundId);
			}
		}
		
		//表情
		private function chatFace(byteArray:ByteArray):void
		{
			var sitId:int = int(readString(byteArray));
			var faceKey:String = readString(byteArray);
			_model.sitModel.updateFace(sitId,faceKey);
		}
		
		//记分牌
		private function roundResult(byteArray:ByteArray):void
		{
			var roundResultData:RoundResultData = new RoundResultData();
			roundResultData.winnerSitId = byteArray.readShort();
			roundResultData.fireSitId = byteArray.readShort();
			
			var count:int = byteArray.readShort();
			
			for (var i:int=0; i<count; i++)
			{
				var resultData:ResultData = new ResultData();
				resultData.sitId = byteArray.readShort();
				resultData.playerName = readString(byteArray);
				resultData.moneyChange = byteArray.readInt();
				resultData.resultState = byteArray.readShort();
				
				roundResultData.resultDatas.push(resultData);
			}
			_model.tableModel.updateResult(roundResultData);
		}
		
		//清楚出牌
		private function clearRoundShowCards(byteArray:ByteArray):void
		{
			_model.tableModel.clearRoundShowCards();
			_model.traceManager.out("《《《《清空牌面");
		}
		
		//房间剩牌，比率
		private function multiLeftCards(byteArray:ByteArray):void
		{
			var multi:String = readString(byteArray);
			var remainCards:String = readString(byteArray);
			_model.tableModel.updateMultiRemainCards(remainCards,multi);
			_model.traceManager.out("房间剩牌:" + remainCards,"房间倍率:" + remainCards);
		} 
		//给自己发牌
		private function givePlayerCards(byteArray:ByteArray):void
		{
			var count:int = byteArray.readShort();
			var cardDatas:Array = [];
			for (var i:int=0; i<count; i++)
			{
				var cardId:int = byteArray.readShort();
				var cardData:ICardData = CardTools.getCardDataById(cardId);
				cardDatas.push(cardData);
			}
			CardTools.sortCards(cardDatas);
			_model.playerModel.showPlayerCards(cardDatas);
		}
		//所有玩家出牌
		private function showTableCards(byteArray:ByteArray):void
		{
			var cardDatas:Array = [];
			var sitId:int = byteArray.readInt();
			var count:int = byteArray.readInt();
			for (var i:int=0; i<count; i++)
			{
				var cardId:int = byteArray.readInt();
				var cardValue:int = byteArray.readInt();
				
				var cardData:ICardData = CardTools.getCardDataByIdAndValue(cardId,cardValue);
				cardDatas.push(cardData);
			}
			_model.tableModel.updateShowCards(sitId,cardDatas);
			_model.traceManager.out("------>所有玩家出牌:" + sitId,"count:"+count);
		}
		
		//座位信息
		private function sitInfo(byteArray:ByteArray):void
		{
			var roleData:IRoleData = new RoleData();
			roleData.id = byteArray.readInt();
			roleData.roleName = readString(byteArray);
			roleData.sex = byteArray.readInt();
			roleData.sitId = byteArray.readInt();
			roleData.nickName = readString(byteArray);
			roleData.imgUrl = readString(byteArray);
			roleData.money = readString(byteArray);
			roleData.vip = byteArray.readShort();
			roleData.snsName = readString(byteArray);
			roleData.homePage = readString(byteArray);
			
			//temp
			//roleData.imgUrl = "headImg/" + Math.floor(5 * Math.random() + 1) + ".jpg";
			
			_model.roleModel.addRoleData(roleData);
			_model.tableModel.playerSit(roleData);
			
			if (roleData.id == GameState.playerId)
			{
				GameState.playerCurrentSitId = roleData.sitId;
				GameState.playerHomePage = roleData.homePage;
				GameState.playerName = roleData.roleName;
				GameState.isSited = true;
			}
		}
		
		//开始一局
		private function startRound(byteArray:ByteArray):void
		{
			var bankerSitId:int = byteArray.readShort();
			_model.tableModel.updateBanker(bankerSitId);
		}
		
		private function sitState(byteArray:ByteArray):void
		{
			var sitId:int = byteArray.readShort();
			var holdCount:int = byteArray.readShort();
			var action:int = byteArray.readShort();
			var leftSecond:int = byteArray.readShort();
			var totalTime:int = byteArray.readShort();
			
			var sitState:ISitState = new SitState();
			sitState.sitId = sitId;
			sitState.holdCount = holdCount;
			sitState.action = action;
			sitState.leftSecond = leftSecond;
			sitState.totalTime = totalTime;
			_model.sitModel.updateSitState(sitState);
			
			_model.traceManager.out("座位状态：","sitId:"+sitId,"holdCount:" + holdCount,"leftSecond:"+ leftSecond,"action:"+ action,"totalTime:"+totalTime);
		}
		//更新托管
		private function autoMsg(byteArray:ByteArray):void
		{
			var sitId:int = byteArray.readShort();
			var autoState:int = byteArray.readShort();
			_model.sitModel.updateSitAutoState(sitId,autoState);
			
			
		}
		//更新钱
		private function moneyMsg(byteArray:ByteArray):void
		{
			var sitId:int = byteArray.readShort();
			var money:String = readString(byteArray);
			_model.sitModel.updateSitMoney(sitId,money);
			_model.traceManager.out("更新钱："+sitId,money);
		}
		//房间信息
		private function handleRoomInfo(byteArray:ByteArray):void
		{
				
			var roomId:int= byteArray.readInt();
			var roomName:String = readString(byteArray);
			var betTime:int = byteArray.readInt();
			var playCondition:int = byteArray.readInt();
			var fee:int = byteArray.readInt();
			var playerId:int = byteArray.readInt();
			
			var roomData:RoomData = new RoomData();
			roomData.id = roomId;
			roomData.roomName = roomName;
			roomData.betTime = betTime;
			roomData.playCondition = playCondition;
			roomData.fee = fee;
			
			GameState.playerId = playerId;
			
			_model.tableModel.updateRoomInfo(roomData);
			
			_model.traceManager.out(roomId,roomName,betTime,playCondition,fee);
		}
		
		
		
		//玩家站起
		public function standUp(byteArray:ByteArray):void
		{
			var sitId:int = int(readString(byteArray));
			
			
			
			_model.sitModel.removeSit(sitId);
		}
	}
}